<?php


namespace api\modules\v1\controllers\buss;

use api\controllers\OnAuthController;
use api\modules\v1\forms\buss\ContentForm;
use common\helpers\CommonHelper;
use services\buss\AccountsService;
use services\buss\ContentPublishRecordService;
use services\buss\ContentService;
use Yii;


class ContentController extends OnAuthController
{
    public $modelClass = '';

    /**
     * @api {get} /v1/buss/content/list  内容列表
     * @apiName list
     * @apiGroup Content
     * @apiUse CommonParam
     *
     * @apiParam {String} created_stime     创建开始时间【格式：YYYY-MM-DD】
     * @apiParam {String} created_etime     创建结束时间【格式：YYYY-MM-DD】
     * @apiParam {String} keywords          关键词
     * @apiParam {String} project_id        项目ID
     * @apiParam {String} tag_ids           标签IDs【数组 or string 多个用','分割】
     * @apiParam {String} type              类型
     * @apiParam {Number} created_member_id 创建人ID
     * @apiParam {Number} c_id              分类
     * @apiParam {Number} publish_status    状态
     * @apiParam {String} order_column      排序字段
     * @apiParam {String} order_sort        排序值【asc正序、desc倒序】
     * @apiParam {String} page              页码
     * @apiParam {String} pageSize          每页展示数
     *
     * @apiSuccess {Object}  data                       返回数据
     * @apiSuccess {Number}  data.page                  页码
     * @apiSuccess {Number}  data.pageSize              每页展示数
     * @apiSuccess {Number}  data.total                 总数
     * @apiSuccess {Array}   data.list                  列表数据
     * @apiSuccess {Number}  data.list.id               ID
     * @apiSuccess {String}  data.list.title            标题
     * @apiSuccess {String}  data.list.project_name     项目名
     * @apiSuccess {String}  data.list.type_str         类型
     * @apiSuccess {Array}   data.list.tag_arr          标签
     * @apiSuccess {String}  data.list.c_name           分类
     * @apiSuccess {Array}   data.list.keyword          关键词
     * @apiSuccess {String}  data.list.publish_status_str  状态
     * @apiSuccess {String}  data.list.is_stream_str    是否投流
     * @apiSuccess {String}  data.list.created_member_name  创建人
     * @apiSuccess {String}  data.list.created_at_str   创建时间
     * @apiSuccess {String}  data.list.change_date      内容更新时间
     * @apiSuccess {Number}  data.list.read_num         浏览量
     * @apiSuccess {Number}  data.list.hd_num           互动数
     * @apiSuccess {Number}  data.list.publish_num      发布次数
     */
    public function actionList()
    {
        $params = Yii::$app->request->get();

        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;

        $service = new ContentService();
        // 查询结果
        $data = $service->getListPage($params, (int)$page, (int)$pageSize);
        // 格式化数据
        $data['list'] = $service->getListPageFormat($data['list']);
        return $data;
    }

    /**
     * @api {get} /v1/buss/content/list-condition  列表筛选条件
     * @apiName list-condition
     * @apiGroup Content
     * @apiUse CommonParam
     *
     * @apiSuccess {Object}  data                   返回数据
     * @apiSuccess {Array}   data.project           项目【{value:'0', label: '项目'}】
     * @apiSuccess {Array}   data.publist_status    发布状态【{value:'0', label: '普通'}】
     * @apiSuccess {Array}   data.type              类型【{value:'0', label: '普通'}】
     * @apiSuccess {Array}   data.created_member    创建人
     * @apiSuccess {Array}   data.tag               标签
     * @apiSuccess {Array}   data.classify          分类
     * @apiSuccess {Array}   data.doctor            医生
     */
    public function actionListCondition()
    {
        $params = Yii::$app->request->get();

        $service = new ContentService();
        return $service->getSelectCondition();
    }

    /**
     * @api {get} /v1/buss/content/info 内容详情
     * @apiName info
     * @apiGroup Content
     * @apiUse CommonParam
     *
     * @apiParam {Number} id 内容ID【新增ID不传，编辑必传ID】
     *
     * @apiSuccess {Object} data                    返回数据
     * @apiSuccess {Number} data.id                 ID
     * @apiSuccess {Number} data.project_id         项目ID
     * @apiSuccess {String} data.type               类型
     * @apiSuccess {String} data.title              标题
     * @apiSuccess {String} data.content            内容
     * @apiSuccess {Array}  data.keyword            关键词
     * @apiSuccess {String} data.c_id               分类ID
     * @apiSuccess {String} data.is_stream          是否投流
     * @apiSuccess {String} data.publish_status     发布状态
     * @apiSuccess {Array}  data.tag_ids            标签IDs
     * @apiSuccess {Array}  data.attachment         附件信息【{file:'', file_url: ''}】
     * @apiSuccess {Array}  data.support_img        支持图片格式【'.jpg', '.gif'】
     * @apiSuccess {Array}  data.support_video      支持视频格式【'.mp4', '.mov'】
     * @apiSuccess {Object} data.condition          筛选条件
     * @apiSuccess {Array}  data.condition.tag      标签
     */
    public function actionInfo()
    {
        $params = Yii::$app->request->get();
        $id = $params['id'] ?? 0;

        // 查询结果
        $service = new ContentService();
        return $service->getInfo((int)$id);
    }

    /**
     * @api {get} /v1/buss/content/download-info 下载内容详情
     * @apiName download-info
     * @apiGroup Content
     * @apiUse CommonParam
     *
     * @apiParam {Number} id 内容ID
     *
     * @apiSuccess {Object} data                    返回数据
     * @apiSuccess {Number} data.id                 ID
     * @apiSuccess {String} data.title              标题
     * @apiSuccess {String} data.content            内容
     * @apiSuccess {Array}  data.attachment         附件信息【{file:'', file_url: ''}】
     */
    public function actionDownloadInfo()
    {
        $params = Yii::$app->request->get();
        $id = $params['id'] ?? 0;

        // 查询结果
        $service = new ContentService();
        return $service->getDownloadInfo((int)$id);
    }

    /**
     * @api {get} /v1/buss/content/publish-info 发布获取筛选详情
     * @apiName publish-info
     * @apiGroup Content
     * @apiUse CommonParam
     *
     *
     * @apiSuccess {Object} data                    返回数据
     * @apiSuccess {Array}  data.doctor             医生【{value:1, label: '普通'}】
     */
    public function actionPublishInfo()
    {
        $params = Yii::$app->request->get();

        // 查询结果
        $service = new ContentService();
        return $service->getPublishInfo();
    }

    /**
     * @api {get} /v1/buss/content/doctor-account-list 根据医生返回账号列表
     * @apiName doctor-account-list
     * @apiGroup Content
     * @apiUse CommonParam
     *
     * @apiParam {Number} doctor_id 医生ID
     *
     * @apiSuccess {Object} data                    返回数据【{value:1, label: '普通'}】
     */
    public function actionDoctorAccountList()
    {
        $params = Yii::$app->request->get();
        $params['doctor_id'] = $params['doctor_id'] ?? 0;
        if (empty($params['doctor_id'])) {
            return [];
        }
        return AccountsService::getEnableConditionList((int)$params['doctor_id']);
    }

    /**
     * @api {get} /v1/buss/content/publish-list 内容发布数据列表
     * @apiName publish-list
     * @apiGroup Content
     * @apiUse CommonParam
     *
     * @apiParam {Number} content_id        内容ID
     * @apiParam {String} page              页码
     * @apiParam {String} pageSize          每页展示数
     *
     * @apiSuccess {Object}  data                       返回数据
     * @apiSuccess {Number}  data.page                  页码
     * @apiSuccess {Number}  data.pageSize              每页展示数
     * @apiSuccess {Number}  data.total                 总数
     * @apiSuccess {Array}   data.list                  列表数据
     * @apiSuccess {Number}  data.list.id               发布ID
     * @apiSuccess {Number}  data.list.content_id       内容ID
     * @apiSuccess {String}  data.list.title            标题
     * @apiSuccess {String}  data.list.platform_name    平台
     * @apiSuccess {String}  data.list.account_name     账号
     * @apiSuccess {String}  data.list.doctor_name      医生
     * @apiSuccess {String}  data.list.read_num         阅读
     * @apiSuccess {String}  data.list.like_num         点赞
     * @apiSuccess {String}  data.list.collect_num      收藏
     * @apiSuccess {String}  data.list.comment_num      评论
     * @apiSuccess {String}  data.list.created_member_name  创建人
     * @apiSuccess {String}  data.list.publish_time_str 发布时间
     * @apiSuccess {String}  data.list.change_date      内容更新时间
     */
    public function actionPublishList()
    {
        $params = Yii::$app->request->get();
        $params['content_id'] = $params['content_id'] ?? 0;
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;

        if (empty($params['content_id'])) {
            return [
                'page' => (int)$page,
                'pageSize' => (int)$pageSize,
                'total' => 0,
                'list' => [],
            ];
        }

        $service = new ContentPublishRecordService();
        // 查询结果
        $data = $service->getListPage($params, (int)$page, (int)$pageSize);
        // 格式化数据
        $data['list'] = $service->getListPageFormat($data['list']);
        return $data;
    }

    /**
     * @api {post} /v1/buss/content/create 新增内容
     * @apiName create
     * @apiGroup Content
     * @apiUse CommonParam
     *
     * @apiBody {Number} project_id         项目ID
     * @apiBody {String} type               类型
     * @apiBody {String} title              标题
     * @apiBody {String} content            内容
     * @apiBody {Array}  keyword            关键词
     * @apiBody {Number} doctor_id          医生ID【选填】
     * @apiBody {Number} c_id               分类ID
     * @apiBody {String} is_stream          是否投流
     * @apiBody {String} publish_status     发布状态
     * @apiBody {Array}  tags               标签【{value: 1, label: '存在标签'},{value: 0, label: '新增标签'}】
     * @apiBody {Array}  attachment         附件信息【'xxx.jpg', 'xx1.jpeg'】
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         内容ID
     */
    public function actionCreate()
    {
        $params = Yii::$app->request->post();

        $service = new ContentService();
        return $service->create($params);
    }

    /**
     * @api {post} /v1/buss/content/update 编辑内容
     * @apiName update
     * @apiGroup Content
     * @apiUse CommonParam
     *
     * @apiBody {Number} id                 ID
     * @apiBody {Number} project_id         项目ID
     * @apiBody {String} type               类型
     * @apiBody {String} title              标题
     * @apiBody {String} content            内容
     * @apiBody {Array}  keyword            关键词
     * @apiBody {Number} doctor_id          医生ID【选填】
     * @apiBody {String} c_id               分类ID
     * @apiBody {String} is_stream          是否投流
     * @apiBody {String} publish_status     发布状态
     * @apiBody {Array}  tags               标签【{value: 1, label: '存在标签'},{value: 0, label: '新增标签'}】
     * @apiBody {Array}  attachment         附件信息【'xxx.jpg', 'xx1.jpeg'】
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         内容ID
     */
    public function actionUpdate()
    {
        $params = Yii::$app->request->post();

        $service = new ContentService();
        return $service->edit($params);
    }

    /**
     * @api {post} /v1/buss/content/delete 删除内容
     * @apiName delete
     * @apiGroup Content
     * @apiUse CommonParam
     *
     * @apiBody {Number} id                内容ID
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         内容ID
     */
    public function actionDelete()
    {
        $params = Yii::$app->request->post();

        $model = new ContentForm();
        $model->setScenario('delete');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        $service = new ContentService();
        return $service->delete($model);
    }

    /**
     * @api {get} /v1/buss/content/download 下载内容
     * @apiName download
     * @apiGroup Content
     * @apiUse CommonParam
     *
     * @apiParam {Number} id 内容ID
     * @apiParam {Number} is_zip 是否下载zip【1是、0否】
     *
     * @apiSuccess {Object} data                    返回数据
     */
    public function actionDownload()
    {
        $params = Yii::$app->request->get();
        $params['is_zip'] = $params['is_zip'] ?? 1;

        $model = new ContentForm();
        $model->setScenario('download');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        // 查询结果
        $service = new ContentService();
        return $service->download($model);
    }
}
